<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Model\HomeTotalModel;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;


class TotalController extends Controller
{
	//虚拟统计首页
	public function index(){
		$list = HomeTotalModel::get();
		 return view('admin.total.index',compact('list'));
	}

	//更改状态
	public function up_state(Request $request){
		$state = $request->post('state');//状态
		$id = $request->post('id');

		DB::beginTransaction();
		try{
			HomeTotalModel::where('id',$id)->update(['state'=>$state]);
			DB::commit();
			$returnArr = ['code'=>1,'msg'=>'操作成功'];
		}catch(Exception $e){
			DB::rollBack();
			$returnArr = ['code'=>0,'msg'=>'操作失败'];
		}
		return response()->json($returnArr);
	}

	//编辑
	public function update(){
		DB::beginTransaction();
		try{
			HomeTotalModel::where('id',request()->id)->update(['value'=>request()->num]);
			DB::commit();
			return ['code'=>1,'msg'=>'编辑成功'];
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'编辑失败'];
		}
	}
}